//
//  PickerView.m
//  cookBook
//
//  Created by xalo on 16/1/26.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#define kLeft 20

#import "PickerView.h"

@interface PickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>


@property (nonatomic,strong) NSMutableArray *pagesArray;

//当前行数
@property (nonatomic,assign) NSInteger currentRow;

@end

@implementation PickerView

- (instancetype)initWithFrame:(CGRect)frame PageNumber:(NSInteger)pageNumber currentRow:(NSInteger )currentRow
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        //frame
        self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(kLeft*3, 200, frame.size.width - 6 * kLeft, 200)];

        //背景颜色
        self.pickerView.backgroundColor = [UIColor grayColor];
        
        //代理
        self.pickerView.delegate = self;
        
        self.pickerView.dataSource = self;
        
        //透明度
        self.pickerView.alpha = 0.8;
        
        //边角
        self.pickerView.layer.cornerRadius = 20;
        
        //添加到父视图
        [self addSubview:self.pickerView];
        
        //设置array,展示的数据
        self.pagesArray = [NSMutableArray array];
        
        for (int i = 1; i < pageNumber + 1; i++) {
            
            NSString *string = [NSString stringWithFormat:@"第 %d 页",i];
            
            [self.pagesArray addObject:string];
            
        }
        
        //当前选中行
        [self.pickerView selectRow:currentRow - 1 inComponent:0 animated:YES];
        
        self.currentRow = currentRow - 1;
        
        
        //添加按钮
        [self addButton];
        
    }
    
    return self;
}

- (void)addButton
{
    //增加确定 取消按钮
    
    //确定
    UIButton *okButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    
    okButton.frame = CGRectMake(kLeft*3+(self.pickerView.frame.size.width - 160)/3-30, self.pickerView.frame.size.height/2 - 22 +200, 80, 44);
    
    [okButton setTitle:@"确定" forState:(UIControlStateNormal)];
    
    [okButton setTintColor:[UIColor greenColor]];
    
    okButton.titleLabel.font = [UIFont systemFontOfSize:20];
    
    [okButton addTarget:self action:@selector(okAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    //取消
    UIButton *cancelButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    
    cancelButton.frame = CGRectMake(self.pickerView.frame.size.width+3*kLeft - (self.pickerView.frame.size.width - 160)/3+30-80, self.pickerView.frame.size.height/2 - 22 +200, 80, 44);
    
    [cancelButton setTitle:@"取消" forState:(UIControlStateNormal)];
    [cancelButton setTintColor:[UIColor greenColor]];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:20];
    
    
    [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self addSubview:okButton];
    [self addSubview:cancelButton];

    
}


#pragma mark -- 按钮

//确定按钮方法
- (void)okAction
{
    //当前选择的行数
    NSInteger number = self.currentRow;
    
    //调用block ,参数number+1
    self.block(number + 1);
    
    //自身页面消失
    [self dismissPickerView];
}

//取消
- (void)cancelAction
{
    [self dismissPickerView];
}

#pragma mark - delegate
// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}



// 行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.pagesArray count];
}



// 每行显示的数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.pagesArray[row];
}



 //选中行数
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    self.currentRow = row;
    
}


//页面消失
- (void)dismissPickerView
{
    [self removeFromSuperview];
}






@end





